package com.syh.jn.scm.infrastructure.repository.impl.supplier.base.goods;

import cn.redscarf.commons.wrapper.Wrapper;
import com.syh.core.ddd.infrastructure.repository.AbstractAggregateRepository;
import com.syh.jn.scm.domain.supplier.entity.base.goods.Origin;
import com.syh.jn.scm.domain.supplier.repository.base.goods.OriginRepository;
import com.syh.jn.scm.dto.supplier.base.goods.goodsOrigin.OriginVo;
import com.syh.jn.scm.infrastructure.repository.model.base.goods.OriginModel;
import com.syh.jn.scm.infrastructure.repository.utils.SQLAliasGenerateUtil;
import com.syh.jn.scm.infrastructure.wrapper.BaseResolve;
import com.syh.jn.scm.infrastructure.wrapper.ResolveToSQL;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;

@Repository
public class OriginRepositoryImpl extends AbstractAggregateRepository<Origin, OriginModel, Long> implements OriginRepository {

  @Override
  protected SQL getViewSql() {
    return new SQL() {{
      SELECT(
              SQLAliasGenerateUtil.aliasGenerate(OriginModel.class, "origin", null)
      );
      FROM("T_BASE_ORIGIN origin");
    }};
  }

  @Override
  protected Class getViewObject() {
    return OriginVo.class;
  }

  @Override
  protected BaseResolve getViewWrapperResolve(Wrapper wrapper) {
    Map<String, String> params = new HashMap<>();
    params.put("id", "origin.ID");
    params.put("name", "origin.NAME");
    params.put("status", "origin.STATUS");
    params.put("code", "origin.CODE");
    params.put("businessEntityId", "origin.BUSINESS_ENTITY_ID");
    ResolveToSQL parse = new ResolveToSQL(wrapper, (propertyName) -> {
      return params.get(propertyName);
    });
    return parse;
  }
}
